//
//  CollectionViewController.m
//  liwo
//
//  Created by liuwenyang on 14-9-19.
//  Copyright (c) 2014年 liuwenyang. All rights reserved.
//

#import "CollectionViewController.h"
#import "MJRefresh.h"

@interface CollectionViewController()



@end


@implementation CollectionViewController


@synthesize collectionView = _collectionView;
@synthesize delegate = _delegate;


-(instancetype)initWithAutoLayout
{
    self = [super init];
    if (self) {
        
        //        _classForItem = classForItem;
        _flowLayout = [[UICollectionViewFlowLayout alloc] init];
        //        flowLayout.itemSize = CGSizeMake([[dic objectForKey:@"cellWidth"] doubleValue] , [[dic objectForKey:@"cellHeight"] doubleValue]);
        _flowLayout.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0);
        _flowLayout.minimumLineSpacing = 0;
        _flowLayout.minimumInteritemSpacing = 0;
        _collectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:_flowLayout];
        //        [_collectionView registerClass:_classForItem forCellWithReuseIdentifier:NSStringFromClass(_classForItem)];
        //    if (isHaveHeaderView) {
        //        flowLayout.headerReferenceSize = CGSizeMake(320, 160);
        //        [_collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"header"];
        //    }
        
        _collectionView.backgroundColor = [UIColor whiteColor];
        _collectionView.delegate = self;
        _collectionView.dataSource = self;
    }
    
    return self;
}


-(instancetype)initWithCellForItem:(Class)classForItem
{
    self = [self initWithAutoLayout];
    if (self) {
        
        _classForItem = classForItem;
        //        [_collectionView registerClass:_classForItem forCellWithReuseIdentifier:NSStringFromClass(_classForItem)];
        [_collectionView registerNib:[UINib nibWithNibName:NSStringFromClass(_classForItem) bundle:nil] forCellWithReuseIdentifier:NSStringFromClass(_classForItem)];
    }
    
    return self;
}




- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    id data = [_dataSource objectAtIndex:indexPath.row];
    Class cellClass = _classForItem;
    return [cellClass sizeForItem:data indexPath:indexPath];
}


-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return _dataSource.count;
}


-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    CollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:NSStringFromClass(_classForItem) forIndexPath:indexPath];
    [cell setValue:[_dataSource objectAtIndex:indexPath.row]];
    cell.itemDelegate = self;
    return cell;
}

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    if (self.delegate && [self.delegate respondsToSelector:@selector(didSelectedCell:dataSrc:index:)]) {
        [self.delegate didSelectedCell:self dataSrc:[self.dataSource objectAtIndex:indexPath.row] index:indexPath];
    }
}






-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 1;
}

-(void)clickElement:(CollectionViewCell *)collectionViewCell element:(id)element ItemData:(id)itemData
{
    if (self.delegate && [self.delegate respondsToSelector:@selector(didClickElementForItem:element:data:)]) {
        [self.delegate didClickElementForItem:self element:element data:itemData];
    }
}

-(void)clickElement:(id)element ItemData:(id)itemData
{
    if (self.delegate && [self.delegate respondsToSelector:@selector(didClickElementForItem:element:data:)]) {
        [self.delegate didClickElementForItem:self element:element data:itemData];
    }
}

//-(Class)classForcell
//{
//    return [CollectionViewCell class];
//}


-(void)dealloc
{
    NSLog(@"%@ dealloc",NSStringFromClass([self class]));
}

@end

@implementation RefleshCollectionViewController


@synthesize pageIndex = pageIndex_;


-(id)initWithDragRefresh:(BOOL)enableDragRefresh withInfiniteScroll:(BOOL)enableInfiniteScroll classForItem:(Class)classForItem
{
    self = [super initWithCellForItem:classForItem];
    if (self) {
        if (enableDragRefresh) {
            self.collectionView.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(refreshHeaderViewDidCallBack)];
            //            self.tableView.mj_header = [DSRefreshHeader headerWithRefreshingTarget:self refreshingAction:@selector(refreshHeaderViewDidCallBack)];
        }
        if (enableInfiniteScroll) {
            self.collectionView.mj_footer  = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(refreshFooterViewDidCallBack)];
        }
        pageIndex_ = 1;
    }
    return self;
}


-(void)forceRefresh
{
    [self refreshHeaderViewDidCallBack];
}


#pragma mark - 顶部的重新刷新
#pragma mark RefreshHeaderViewDelegate

- (void)refreshHeaderViewDidCallBack
{
    //    [_rHeaderView startLoading];
    __weak CollectionViewController *weakSelf = self;
    //    _isAlreadyRefresh = YES;
    self.pageIndex = 1;
    [self refreshTable:^(BOOL finished) {
        if (finished) {
            //            [_rHeaderView stopLoading];
            [weakSelf.collectionView.mj_header endRefreshing];
            if (_totalPages > pageIndex_) {
                weakSelf.collectionView.mj_footer  = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:weakSelf refreshingAction:@selector(refreshFooterViewDidCallBack)];
            }else{
                //                [self.collectionView removeFooter];
            }
            //            pageIndex_ = 1;
            //通知代理model加载完成
            if (_delegate && [_delegate respondsToSelector:@selector(collectionModelDidLoadFinished:)]) {
                [_delegate collectionModelDidLoadFinished:self];
            }
        } else {
            NSLog(@"modelDidLoadFailure");
        }
    }];
}

#pragma mark - 底部的获取更多
#pragma mark RefreshFooterViewDelegate

- (void)refreshFooterViewDidCallBack
{
    //    [_rFooterView startLoading];
    __weak CollectionViewController *weakSelf = self;
    ++self.pageIndex;
    [self getTableMore:^(BOOL finished) {
        if (finished) {
            [weakSelf.collectionView.mj_footer endRefreshing];
            //            [_rFooterView stopLoading];
        }
        if (_totalPages > pageIndex_) {
            weakSelf.collectionView.mj_footer  = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:weakSelf refreshingAction:@selector(refreshFooterViewDidCallBack)];
        }else{
            //            [self.collectionView removeFooter];
        }
        //        pageIndex_ += 1;
        //通知代理model加载完成
        if (_delegate && [_delegate respondsToSelector:@selector(collectionModelDidLoadFinished:)]) {
            [_delegate collectionModelDidLoadFinished:self];
        }
    }];
}

-(UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
    UIView *view = nil;
    UICollectionReusableView *headerView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"header" forIndexPath:indexPath];
    if (self.delegate && [self.delegate respondsToSelector:@selector(viewForcollectionView:)]) {
        view = [self.delegate viewForcollectionView:self];
    }
    [headerView addSubview:view];
    return headerView;
}

-(void)getTableMore:(void (^)(BOOL))block
{
    
}

-(void)refreshTable:(void (^)(BOOL))block
{
    
}

@end




















